package com.file.system.service.impl.system;

import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.file.system.entity.res.DataPage;
import com.file.system.entity.system.WebDepartmentUser;
import com.file.system.entity.system.WebUser;
import com.file.system.mapper.system.WebDepartmentUserMapper;
import com.file.system.mapper.system.WebUserMapper;
import com.file.system.plugin.shiro.ShiroTools;
import com.file.system.service.system.IWebUserService;
import com.file.system.tools.page.DP;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.List;

/**
 * 账号 实现
 *
 * @author jch
 * @since 2022-08-02
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class WebUserServiceImpl implements IWebUserService {

    @Autowired
    private WebUserMapper mapper;
    @Autowired
    private WebDepartmentUserMapper webDepartmentUserMapper;

    //添加账号
    public boolean save(WebUser webUser) {
        webUser.setId(IdUtil.getSnowflakeNextId());
        return SqlHelper.retBool(mapper.save(webUser));
    }


    //删除账号
    public boolean removeById(@Param("id") Long id) {
        //删除账号
        int state = mapper.removeById(new WebUser().setId(id));
        if (state < 1) {
            return false;
        }
        //删除账号关联信息
        return SqlHelper.retBool(webDepartmentUserMapper.removeById(id));
    }

    //通过ids删除账号
    public boolean removeByIds(List<Long> ids) {
        UpdateWrapper<WebUser> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("is_del", 1);
        updateWrapper.set("delete_by", ShiroTools.getId());
        updateWrapper.set("delete_name", ShiroTools.getName());
        updateWrapper.set("delete_time", LocalDateTime.now());
        updateWrapper.in("id", ids);


        int state = mapper.update(updateWrapper);
        if (state < 1) {
            return false;
        }
        return true;
    }

    //编辑账号
    public boolean updateById(@Param("et") WebUser webUser) {
        if (ObjectUtil.isEmpty(webUser.getId())) {
            return false;
        }

        if (ObjectUtil.isNotNull(webUser.getFkDepartmentId())) {
            //删除旧用户部门关联
            webDepartmentUserMapper.removeById(webUser.getId());
            //添加部门用户关联
            WebDepartmentUser departmentUser = new WebDepartmentUser();
            departmentUser.setId(IdUtil.getSnowflakeNextId());
            departmentUser.setFkDepartmentId(webUser.getFkDepartmentId());
            departmentUser.setFkUserId(webUser.getId());
            webDepartmentUserMapper.save(departmentUser);
        }
        return SqlHelper.retBool(mapper.updateById(webUser));
    }

    //编辑
    public boolean update(@Param("ew") UpdateWrapper<WebUser> query) {
        return SqlHelper.retBool(mapper.update(query));
    }

    //添加账号(批量)
    public boolean saveBatch(@Param("list") List<WebUser> webUserList) {
        if (webUserList.size() < 1) {
            return false;
        }
        return SqlHelper.retBool(mapper.saveBatch(webUserList));
    }

    //查询账号
    @DS("slave")
    public List<WebUser> list(@Param("ew") QueryWrapper<WebUser> query) {
        return mapper.select(query);
    }

    //通过ids查询账号集合
    @DS("slave")
    public List<WebUser> listByIds(List<Long> ids) {
        QueryWrapper<WebUser> query = new QueryWrapper<>();
        query.in("a.id", ids);
        return mapper.select(query);
    }

    //查询所有非删除状态账号
    @DS("slave")
    public List<WebUser> listAll() {
        QueryWrapper<WebUser> query = new QueryWrapper<>();
        query.eq("a.is_del", 0);
        query.orderByDesc("a.id");
        return mapper.select(query);
    }

    //查询账号分页
    @DS("slave")
    public DataPage page(@Param("pageIndex") Integer pageIndex, @Param("pageSize") Integer pageSize, @Param("ew") QueryWrapper<WebUser> query) {
        //查询数量
        int count = mapper.selectCount(query);
        DP dp = DP.init().paging(pageIndex, pageSize, count);
        Page<WebUser> page = new Page<>(dp.getPid(), dp.getPageSize());
        page.setSearchCount(false);
        List<WebUser> list = mapper.select(page, query);
        return DataPage.init().set(count, list);
    }

    //通过id查询账号
    @DS("slave")
    public WebUser getById(@Param("id") Long id) {
        return mapper.selectById(id);
    }

    //通过条件查询账号
    @DS("slave")
    public WebUser getOne(@Param("ew") QueryWrapper<WebUser> query) {
        List<WebUser> list = mapper.select(query);
        return list.size() > 0 ? list.get(0) : null;
    }

    //查询账号数量
    @DS("slave")
    public int count(@Param("ew") QueryWrapper<WebUser> query) {
        return mapper.selectCount(query);
    }
}
